import UIKit
import Down

public class HTMLTagHandler {
    // 支持的HTML标签类型
    public enum TagType {
        case div
        case span
        case sup
        case sub
        case mark
        case ruby
        case rt
        case custom(String)
        
        var tagName: String {
            switch self {
            case .div: return "div"
            case .span: return "span"
            case .sup: return "sup"
            case .sub: return "sub"
            case .mark: return "mark"
            case .ruby: return "ruby"
            case .rt: return "rt"
            case .custom(let name): return name
            }
        }
    }
    
    // HTML标签属性
    public struct TagAttributes {
        var id: String?
        var classes: [String]
        var style: [String: String]
        var customAttributes: [String: String]
        
        public init(
            id: String? = nil,
            classes: [String] = [],
            style: [String: String] = [:],
            customAttributes: [String: String] = [:]
        ) {
            self.id = id
            self.classes = classes
            self.style = style
            self.customAttributes = customAttributes
        }
    }
    
    // 处理HTML标签
    public static func processHTMLTag(_ tag: TagType, attributes: TagAttributes, content: String) -> NSAttributedString {
        let htmlString = buildHTMLString(tag: tag, attributes: attributes, content: content)
        
        // 使用Down解析HTML
        guard let attributedString = try? Down(markdownString: htmlString)
            .toAttributedString() else {
            return NSAttributedString(string: content)
        }
        
        return attributedString
    }
    
    // 构建HTML字符串
    private static func buildHTMLString(tag: TagType, attributes: TagAttributes, content: String) -> String {
        var attributesString = ""
        
        // 添加ID
        if let id = attributes.id {
            attributesString += " id=\"\(id)\""
        }
        
        // 添加类
        if !attributes.classes.isEmpty {
            attributesString += " class=\"\(attributes.classes.joined(separator: " "))\""
        }
        
        // 添加样式
        if !attributes.style.isEmpty {
            let styleString = attributes.style
                .map { "\($0.key): \($0.value)" }
                .joined(separator: "; ")
            attributesString += " style=\"\(styleString)\""
        }
        
        // 添加自定义属性
        attributes.customAttributes.forEach { key, value in
            attributesString += " \(key)=\"\(value)\""
        }
        
        return "<\(tag.tagName)\(attributesString)>\(content)</\(tag.tagName)>"
    }
    
    // 上标
    public static func superscript(_ text: String) -> NSAttributedString {
        processHTMLTag(.sup, attributes: TagAttributes(), content: text)
    }
    
    // 下标
    public static func `subscript`(_ text: String) -> NSAttributedString {
        processHTMLTag(.sub, attributes: TagAttributes(), content: text)
    }
    
    // 高亮
    public static func highlight(_ text: String, color: UIColor) -> NSAttributedString {
        let attributes = TagAttributes(
            style: ["background-color": color.cssColor]
        )
        return processHTMLTag(.mark, attributes: attributes, content: text)
    }
    
    // 注音
    public static func ruby(_ text: String, annotation: String) -> NSAttributedString {
        let rubyContent = "\(text)<rt>\(annotation)</rt>"
        return processHTMLTag(.ruby, attributes: TagAttributes(), content: rubyContent)
    }
    
    // 自定义样式的span
    public static func styledSpan(_ text: String, style: [String: String]) -> NSAttributedString {
        let attributes = TagAttributes(style: style)
        return processHTMLTag(.span, attributes: attributes, content: text)
    }
} 
